//
//  PokeManListView.swift
//  PokeMaster
//
//  Created by 李腾芳 on 2022/1/8.
//  Copyright © 2022 OneV's Den. All rights reserved.
//

import SwiftUI

struct PokeManListView: View {
    @EnvironmentObject var store: Store
    
    var body: some View {
        NavigationView {
            if store.appState.home.needRetry {
                Button(action: {
                    self.store.dispatch(AppAction.home(.loadPokeMenList))
                }, label: {
                    Image(systemName: "arrow.clockwise")
                    Text("retry")
                })
                    .foregroundColor(Color.gray)
                    .padding()
                    .overlay {
                        RoundedRectangle(cornerRadius: 5)
                            .stroke(Color.gray, lineWidth: 1)
                    }
            } else {
                if store.appState.home.list == nil {
                    Text("加载中。。。。。")
                        .onAppear {
                            self.store.dispatch(AppAction.home(.loadPokeMenList))
                        }
                        .navigationTitle("宝可梦列表")
                } else {
                    List(PokemonViewModel.all) { model in
                        Group {
                            PokemenInfoRow(model: model, isExpand: store.appState.home.expandingIndex == model.id)
                                    //   .animation(.default)
                            
                        }.onTapGesture {
                            self.store.dispatch(AppAction.home(.expandPokemonRow(id: model.id)))
                        }
                      //  .task(priority: <#T##TaskPriority#>, <#T##action: () async -> Void##() async -> Void#>)
                        
                    }
//                    .overlay {
//                        PokeManInfoPanelManager(model: store.appState.home.detailPokeMonViewModel)
//                            .animation(.interpolatingSpring(stiffness: 70, damping: 12))
//                    }
                    .overlaySheet(isPresented: $store.appState.home.presentPokemonPannel,
                                  content: {
                        if let model = store.appState.home.detailPokeMonViewModel {
                            //     PokemenInfoRow(model: model, isExpand: model.id == store.appState.home.expandingIndex)
                            PokeManInfoPannel(model: model)
                        }
                        
                    })
                    .navigationTitle("宝可梦列表")
                }
            }
        }
    }
}

struct PokeManListView_Previews: PreviewProvider {
    static var previews: some View {
        PokeManListView()
    }
}
